<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996 15899929162
// +----------------------------------------------------------------------
namespace app\common\dao\room;

use app\common\dao\BaseDao;
use app\common\model\room\RoomOwnerModel;
use app\helper\Time;

class RoomOwnerDao extends BaseDao
{

    /**
     * @return RoomOwnerModel
     */
    protected function getModel(): string
    {
        return RoomOwnerModel::class;
    }

    public function search(array $where)
    {
        return $this->getModel()::getDB()
            ->when(isset($where['property_id']) && $where['property_id'] !== '', function ($query) use ($where) {
                if (is_array($where['property_id'])) {
                    $query->where('property_id', 'in', $where['property_id']);
                } else {
                    $query->where('property_id', (int)$where['property_id']);
                }
            })
            ->when(isset($where['audit_status']) && $where['audit_status'] !== '', function ($query) use ($where) {
                $query->where('audit_status', (int)$where['audit_status']);
            })
            ->when(isset($where['owner_type']) && $where['owner_type'] !== '', function ($query) use ($where) {
                $query->where('owner_type', (int)$where['owner_type']);
            })
            ->when(isset($where['username']) && $where['username'] !== '', function ($query) use ($where) {
                $query->whereIn('user_id', function ($query) use ($where) {
                    $query->name('users')->whereLike('username|mobile', '%' . trim($where['username']) . '%')->field('id');
                });
            })
            ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
                $query->whereLike('remark|floor_name|room_name', '%' . trim($where['keywords']) . '%');
            });
    }

    /**
     * 业主数量统计
     * @param int $itemsId
     * @param $day
     * @return int
     * @throws \think\db\exception\DbException
     *
     * @author Amos
     * 2024-07-29 14:40
     */
    public function ownerStatistics(int $itemsId = 1, $day = null)
    {
        return $this->getModel()::getDB()->where('items_id', $itemsId)
            ->when(isset($day) && $day !== '', function ($query) use ($day) {
                if ($day == 'today') {
                    $query->whereBetween('add_time', Time::today(true));
                } elseif ($day == 'yesterday') {
                    $query->whereBetween('add_time', Time::yesterday(true));
                } elseif ($day == 'day') {
                    $query->whereBetween('add_time', [date('Y-m-d 00:00:00', strtotime($day)), date('Y-m-d 23:59:59', strtotime($day))]);
                }
            })->count();
    }
}
